package com.vote.services.impl;

import org.apache.log4j.Logger;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.math.NumberUtils;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;

import com.vote.model.Comment;
import com.vote.services.CommentService;
import com.vote.thrift.utils.CommentUtils;
import com.vote.thrift.utils.TComment;

public class CommentServiceCassandraRemoteImpl implements CommentService{
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(CommentServiceCassandraRemoteImpl.class);
	
	private String host;
	private String port;
	private TTransport transport;
	private TProtocol protocol;
	private CommentUtils.Client client;
	
	public void init(){
		transport = new TSocket(host, NumberUtils.toInt(port));
		protocol = new TBinaryProtocol(transport);
		client = new CommentUtils.Client(protocol);
		try {
			transport.open();
		} catch (TTransportException e) {
			logger.error("open transport to thrift service fail~~", e);
			return;
		}
		logger.info("open transport to thrift service success~~");
	}
	
	public void destroy(){
		try {
			transport.flush();
		} catch (TTransportException e) {
			logger.error("flush thrift transport fail~~",e);
		}
		transport.close();
		logger.info("close thrift transport success~~");
	}
	@Override
	public List<Comment> getCommentListByQuestionnaireId(int questionnaireId,
			int offset, int length) {
		List<TComment> tclist = null;
		try {
			tclist = client.getCommentListByQuestionnaireId(questionnaireId, offset, length);
		} catch (TException e) {
			logger.error("get comment list from thrift service fail ~~~", e);
			e.printStackTrace();
		}
		List<Comment> clist = new ArrayList<Comment>();
		for(TComment tc : tclist){
			Comment c = TComment2Comment(tc);
			clist.add(c);
		}
		return clist;
	}

	@Override
	public int getCommentListCountByQuestionnaireId(int questionnaireId) {
		int count = 0;
		try {
			count = client.getCommentListCountByQuestionnaireId(questionnaireId);
		} catch (TException e) {
			logger.error("get comment count from thrift service fail~~", e);
		}
		return count;
	}

	@Override
	public int insertComment(Comment comment) throws Exception {
		TComment tc = Comment2TComment(comment);

		try {
			return client.insertComment(tc);
		} catch (TException e) {
			logger.error("insert comment into thrift service fail ~~~", e);
			return -1;
		}
	}
	
	private Comment TComment2Comment(TComment tc){
		Comment c = new Comment();
		c.setCommentText(tc.getCommentText());
		c.setCommentTime(new Timestamp(tc.getCommentTime()));
		c.setFromUserId(tc.getFromUserId());
		c.setToUserId(tc.getToUserId());
		c.setQuestionnaireId(tc.getQuestionnaireId());
		return c;
	}
	
	private TComment Comment2TComment(Comment c){
		TComment tc = new TComment();
		tc.setCommentText(c.getCommentText());
		tc.setCommentTime(c.getCommentTime().getTime());
		tc.setFromUserId(c.getFromUserId());
		tc.setToUserId(c.getToUserId());
		tc.setQuestionnaireId(c.getQuestionnaireId());
		return tc;
	}

	public String getHost() {
		return host;
	}
	public void setHost(String host) {
		this.host = host;
	}
	public String getPort() {
		return port;
	}
	public void setPort(String port) {
		this.port = port;
	}
}
